package huzh.foundation.appserver;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;

/**
 *<p>title:AbstractAppServerService</p>
 *<p>description:</p>
 * @since 2014-6-8
 * @author huzh
 * @version 1.0
 */
public abstract class AbstractAppServerService {
    /**url*/
    protected static String url = null;
    /**userName*/
    protected static String userName = null;
    /**password*/
    protected static String password = null;
    /**local*/
    protected static boolean local = true;
    /**ENV_JNDI_NAME*/
    public static final String ENV_JNDI_NAME = "java:comp/env";

    /**
     * 
     */
    public static void init(){
        url = AppServerEnv.getInstance().getAppserverUrl();
        userName = AppServerEnv.getInstance().getAppserverUserName();
        password = AppServerEnv.getInstance().getAppserverPassword();
        local = AppServerEnv.getInstance().isLocal();
    }

    /**
     * @param ctx
     * @param dsJNDIName
     * @return
     * @throws NamingException
     */
    public static DataSource getDataSource(Context ctx, String dsJNDIName) throws NamingException{
        return (DataSource)ctx.lookup(dsJNDIName);
    }

    /**
     * @param paramString
     * @return
     * @throws NamingException
     */
    public abstract DataSource getDataSource(String paramString) throws NamingException;

    /**
     * @param ctx
     * @param utxJNDIName
     * @return
     * @throws NamingException
     */
    public static UserTransaction getUserTx(Context ctx, String utxJNDIName) throws NamingException{
        return (UserTransaction)ctx.lookup(utxJNDIName);
    }

    /**
     * @return
     * @throws NamingException
     */
    public abstract UserTransaction getUserTx() throws NamingException;

    /**
     * @param ctx
     * @return
     * @throws NamingException
     */
    public static Context getEnvContext(Context ctx) throws NamingException{
        return (Context)ctx.lookup("java:comp/env");
    }

    /**
     * @return
     * @throws NamingException
     */
    public abstract Context getEnvContext() throws NamingException;

    /**
     * @return
     * @throws NamingException
     */
    public abstract InitialContext getInitialContext() throws NamingException;
}
